package logic

import (
	"context"
	"zhaowei/models/mysql"

	"zhaowei/baidurpc/baidurpc"
	"zhaowei/baidurpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LikeAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLikeAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LikeAddLogic {
	return &LikeAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LikeAddLogic) LikeAdd(in *baidurpc.LikeAddRequest) (*baidurpc.LikeAddResponse, error) {
	// todo: add your logic here and delete this line
	var like mysql.Like
	mysql.DB.Debug().Where("post_id = ?", in.PostId).Where("user_id = ?", in.UserId).Find(&like)
	like.PostId = int(in.PostId)
	like.UserId = int(in.UserId)
	mysql.DB.Debug().Create(&like)
	var po mysql.Post
	mysql.DB.Debug().Where("id = ?", in.PostId).Find(&po)
	po.Like = po.Like + 1
	mysql.DB.Debug().Updates(&po)
	return &baidurpc.LikeAddResponse{Id: int64(like.ID)}, nil
}
